#include "link/include/factory.h"
int sql_init(MYSQL *conn)
{
    const char* server="localhost";
    const char* user="root";
    const char* password="root";
    const char* database="netdisc";//要访问的数据库名称
    if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
    {
        printf("Error connecting to database: %s\n",mysql_error(conn));
        return -1;
    }else{
        printf("Mysql server connected...\n");
    }
    return 0;
}
int sql_query(MYSQL *conn,MYSQL_RES **res,char *query,unsigned int *num)
{//这里res必须要传递二级指针
    *num=mysql_query(conn,query);
    if(*num)
    {
        printf("Error making query: %s\n",mysql_error(conn));
        return -1;
    }else{
        //	printf("Query made...\n");
        *res=mysql_use_result(conn);
    }
    return 0;
}
int sql_insert(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query: %s\n",mysql_error(conn));
        return -1;
    }else{
        printf("insert success\n");
    }
    return 0;

}
int sql_delete(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query: %s\n",mysql_error(conn));
        return -1;
    }else{
        printf("delete success, delete row = %ld\n",(long)mysql_affected_rows(conn));
    }
    return 0;
}
int sql_update(MYSQL *conn,char *query)
{
    int num;
    num=mysql_query(conn,query);
    if(num)
    {
        printf("Error making query: %s\n",mysql_error(conn));
        return -1;
    }else{
        printf("update success\n");
    }
    return 0;
}
